<template>
  <!-- 放置弹层组件 -->
  <el-dialog :title="showtitle" :visible="showDialog" @close="closefn" @open="getListAPi">
    <el-form ref="addpets" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in peoples" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="addfn">确定</el-button>
      <el-button size="small" @click="closefn">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getListAPi, addList, AgainAddList } from '@/api/departments'
export default {
  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    noteDate: {
      type: Object,
      default: () => {}
    },
    dataList: {
      type: Array,
      default: () => {}
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: this.validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: this.validateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      peoples: []
    }
  },
  computed: {
    showtitle() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getListAPi()
  },
  methods: {
    closefn() {
      this.$emit('closefn')
      this.$refs.addpets.resetFields()
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '', // 部门介绍
        pid: ''
      }
    },
    /**
     * 检验部门名称
     */
    validateName(rule, value, callback) {
      const children = this.noteDate.children || []
      const flag = children.some(item => item.name === value)
      flag ? callback(new Error('部门名称不能重复')) : callback()
    },
    /**
     * 校验部门编码
     */
    validateCode(rule, value, callback) {
      if (this.form.id && this.form.code === value) {
        callback()
        return
      } else {
        const flag = this.dataList.some(item => item.code === value)
        flag ? callback(new Error('部门编码不能重复')) : callback()
      }
    },
    async getListAPi() {
      const res = await getListAPi()
      console.log(res)
      this.peoples = res.data
    },
    addfn() {
      this.$refs.addpets.validate(async(flag) => {
        if (flag) {
          if (this.form.id) {
            const res = await AgainAddList(this.form)
            console.log(res)
            this.$message.success('修改成功')
          } else {
            this.form.pid = this.noteDate.id
            const res = await addList(this.form)
            console.log(res)
            this.$message.success('添加成功')
          }
          this.closefn()
          this.$parent.userList()
        }
      })
    }
  }
}
</script>
